package com.nt.train.controller;
import com.demo.bean.CarInfo;
import com.demo.bean.ComputerInfo;
import com.demo.bean.Mobile;
import com.demo.bean.UserInfo;
import com.nt.train.dto.Book;
import com.nt.train.dto.Car;
import com.nt.train.dto.CarFactory;
import com.nt.train.dto.Orders;
import com.nt.train.utils.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * https://blog.csdn.net/J080624/article/details/53503176
 * @author hlf
 * @version 1.0
 * @date 2022/7/28 8:51
 */
@RestController
@RequestMapping(value = "/index")
public class IndexController {

    //传统方法1
    @RequestMapping(value = "/testBean", method = RequestMethod.GET)
    public Map second(){
        Map<String,Object>  resultMap = new HashMap<>();
        resultMap.put("静态方法Car", CarFactory.getCar());

        CarFactory carFactory = new CarFactory();
        resultMap.put("普通方法Car",carFactory.getNewCar());
        resultMap.put("success","success");
        return resultMap;
    }

    //xml模式
    @RequestMapping(value = "/testBeanXml", method = RequestMethod.GET)
    public Map testBeanXml(){
        Map<String,Object>  resultMap = new HashMap<>();
        //set注入
        Book book =(Book) SpringUtil.getBean( "book", Book.class );
        resultMap.put("set注入Book", book);

        //构造函数
        //Orders order = SpringUtil.getBean( "orders", Orders.class );
        Orders order =(Orders) SpringUtil.getBean("orders");
        resultMap.put("构造函数Orders", order);

        //p空间注入
        Car car =  SpringUtil.getBean( "car", Car.class );
        resultMap.put("p空间注入Car", car);

        //静态工厂注入
        Car car2 = SpringUtil.getBean( "car2", Car.class );
        resultMap.put("静态工厂注入XmlCar", car2);

        //实例工厂注入
        Car car3 = SpringUtil.getBean( "cars", Car.class );
        resultMap.put("实例工厂注入注入XmlCar", car3);

        resultMap.put("success","success");
        return resultMap;
    }


    //不同路径下包名处理扫描bean
    @RequestMapping(value = "/testScanPackage", method = RequestMethod.GET)
    public Map testScanPackage(){
        Map<String,Object>  resultMap = new HashMap<>();
        resultMap.put("不同路径包注入bean扫描测试",true);
        CarInfo carInfo = SpringUtil.getBean("carInfo", CarInfo.class);
        resultMap.put("carInfo",carInfo);
        Mobile mobile = SpringUtil.getBean("mobile", Mobile.class);
        resultMap.put("mobile",mobile);

        ComputerInfo computerInfo = SpringUtil.getBean("computerInfo", ComputerInfo.class);
        computerInfo.setName("联想IBM");
        computerInfo.setBrand("戴维斯");
        resultMap.put("computerInfo",computerInfo);

        UserInfo userInfo = SpringUtil.getBean("userInfo", UserInfo.class);
        //userInfo.setComputerInfo(computerInfo);
//        userInfo.setMobile(mobile);
//        userInfo.setCarInfo(carInfo);
        resultMap.put("userInfo",userInfo);
        resultMap.put("success","success");
        return resultMap;
    }




}
